package com.itheima.controller;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.constant.RedisMessageConst;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    /*
     * 1.调用工具类生成验证码
     * 2.把验证码存储到redis中
     * 3.发送验证码到用户手机
     * */
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone) {
        try {
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
            jedisPool.getResource().setex(RedisMessageConst.SENDTYPE_ORDER + "-" + telephone, 5 * 60, String.valueOf(validateCode));
            //3.发送验证码到用户手机
            // SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, String.valueOf(validateCode));

            return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
        }
    }

    /*
     * 1.调用工具类生成验证码
     * 2.把验证码存储到redis中
     * 3.发送验证码到用户手机
     *
     * */
    @RequestMapping("/send4Login")
    public Result send4Login(String telephone) {
        try {

            String validateCode = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
            jedisPool.getResource().setex(RedisMessageConst.SENDTYPE_LOGIN + "-" + telephone, 5 * 60, validateCode);
//            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, validateCode);
            return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.SEND_VALIDATECODE_FAIL);
        }
    }
}
